package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.SysParam;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface SysParamsMapper {

    @Select("<script>" +
            "SELECT  \n" +
            " temp.PARAM_EN_NAME paramEnName,  \n" +
            " temp.PARAM_CN_NAME paramCnName,  \n" +
            " temp.PARAM_VALUE paramValue,\n" +
            " temp.PARAM_DESC paramDesc,\n" +
            " temp.CREAT_TIME creatTime,\n" +
            " temp.CREAT_USER creatUser\n" +
            "FROM (   \n" +
            "\tSELECT  \n" +
            "\t\tROWNUM rowno,  \n" +
            "\t\tt.*  \n" +
            "\tFROM T_SYS_PARAM t\n" +
            "\tWHERE (t.PARAM_EN_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') " +
            "OR t.PARAM_CN_NAME LIKE CONCAT(CONCAT('%', #{query}),'%')" +
            "OR t.PARAM_VALUE LIKE CONCAT(CONCAT('%', #{query}),'%')" +
            ") \n" +
            "\tAND ROWNUM &lt;= #{pagesize}  ORDER BY t.PARAM_EN_NAME\n" +
            ") temp WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<SysParam> findSysParamsList(@Param("query") String query, @Param("start") int start, @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.PARAM_EN_NAME) FROM T_SYS_PARAM a WHERE " +
            "a.PARAM_EN_NAME LIKE CONCAT(CONCAT('%', #{query}),'%')\n" +
            "OR a.PARAM_CN_NAME LIKE CONCAT(CONCAT('%', #{query}),'%')\n" +
            "OR a.PARAM_VALUE LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countSysParamsList(String query);

    @Insert("<script>" +
            "INSERT INTO T_SYS_PARAM \n" +
            "(PARAM_EN_NAME,PARAM_CN_NAME,PARAM_VALUE,CREAT_TIME,CREAT_USER" +
            "<if test=\" paramDesc != null\">"+
            ",PARAM_DESC" +
            "</if>"+
            ") \n" +
            "VALUES \n" +
            "(#{paramEnName},#{paramCnName},#{paramValue},sysdate,#{creatUser}" +
            "<if test=\" paramDesc != null\">"+
            ",#{paramDesc}" +
            "</if>"+
            ")" +
            "</script>")
    void addSysParamsInfo(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.PARAM_EN_NAME paramEnName,  \n" +
            " temp.PARAM_CN_NAME paramCnName,  \n" +
            " temp.PARAM_VALUE paramValue,\n" +
            " temp.PARAM_DESC paramDesc\n" +
            "FROM T_SYS_PARAM temp\n" +
            "\tWHERE temp.PARAM_EN_NAME = #{paramEnName}\n" +
            "</script>")
    SysParam findSysParamsInfoByParamEnName(String paramEnName);

    @Update("<script>" +
            "UPDATE T_SYS_PARAM SET \n" +
            "\tPARAM_EN_NAME = #{paramEnName},\n" +
            "\tPARAM_CN_NAME = #{paramCnName},\n" +
            "\tPARAM_VALUE = #{paramValue},\n" +
            "\tCREAT_TIME = sysdate,\n" +
            "\tCREAT_USER = #{creatUser}\n" +
            "<if test=\" paramDesc != null\">"+
            "\t,PARAM_DESC = #{paramDesc}\n" +
            "</if>"+
            "WHERE PARAM_EN_NAME = #{oldSysParamsEnName}" +
            "</script>")
    void updateSysParamsInfo(JSONObject params);

    @Delete("<script>DELETE FROM T_SYS_PARAM WHERE PARAM_EN_NAME = #{paramsEnName}</script>")
    void removeSysParamsInfo(String paramsEnName);
}
